<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends MY_Controller {
	function __construct(){
		parent::__construct();
	}

	function index(){
		//Get truck post
		if(!$data = $this->cache->get("post_home")){
			$this->load->model("post_model","postModel");
			$this->load->model('images_model','imagesModel');
			$this->load->model("category_post_model","categoryPostModel");
			$this->load->model("category_model","categoryModel");
			$this->load->model("group_model","groupModel");
			$this->load->model('type_model','typeModel');
			$where['where']['status'] = 1;
			$data["allPosts"]["total"] = $this->postModel->count_rows($where);
			$where["limit"] = 20;
			$where["order"] = "update_date DESC";
			$data["allPosts"]["list"] = $this->postModel->get($where);

			if(!empty($data["allPosts"]["list"])){
				foreach ($data["allPosts"]["list"] as $key => $value) {
					$data["allPosts"]["list"][$key]["categories"] = array_reverse(Util::getCategoryInherit($value["cat_id"]));
					$data["allPosts"]["list"][$key]["province"] = Util::getProvince($data["allPosts"]["list"][$key]["province"]);
					$data["allPosts"]["list"][$key]["type"] = Util::getType($data["allPosts"]["list"][$key]["typ_id"]);
					$data["allPosts"]["list"][$key]["images"] = $this->imagesModel->get(array("where"=>array("pos_id"=>$value["id"])));
					$data["allPosts"]["list"][$key]["meta"] = Util::getPostMeta($data["allPosts"]["list"][$key]["id"]);
					$data["allPosts"]["list"][$key]["comment_count"] = Util::getCommentCount($data["allPosts"]["list"][$key]["id"]);
				}
			}else{
				$data["allPosts"]["list"] = array();
			}

			$data["truckPosts"] = self::_getPostByCategoryID(array(15,16,17,18));
			$data["headPosts"] = self::_getPostByCategoryID(array(24));
			$data["trailerTruckPosts"] = self::_getPostByCategoryID(array(19,20,21,22,23));
			$data["trailerPosts"] = self::_getPostByCategoryID(array(27));
			$data["tractorTrailerPosts"] = self::_getPostByCategoryID(array(25));
			$data["heavyEquipmentPosts"] = self::_getPostByCategoryID(array(32));
			$data["busPosts"] = self::_getPostByCategoryID(array(28));
			$data["farmPosts"] = self::_getPostByCategoryID(array(757));

			$this->cache->write($data,"post_home",120);
		}

		//Get All post
		$this->_assign("allPosts",$data["allPosts"]["list"]);
		$this->_assign("totalPost",$data["allPosts"]["total"]);


		$this->_assign("truckPosts",$data["truckPosts"]["list"]);
		$this->_assign("totalTruckPost",$data["truckPosts"]["total"]);

		//Get head post
		
		$this->_assign("headPosts",$data["headPosts"]["list"]);
		$this->_assign("totalHeadPost",$data["headPosts"]["total"]);

		//Get Trailer truck post
		
		$this->_assign("trailerTruckPosts",$data["trailerTruckPosts"]["list"]);
		$this->_assign("totalTrailerTruckPost",$data["trailerTruckPosts"]["total"]);

		//Get Trailer truck post
		
		$this->_assign("trailerPosts",$data["trailerPosts"]["list"]);
		$this->_assign("totalTrailerPosts",$data["trailerPosts"]["total"]);

		//Get Tractor-trailer post
		
		$this->_assign("tractorTrailerPosts",$data["tractorTrailerPosts"]["list"]);
		$this->_assign("totalTractorTrailerPost",$data["tractorTrailerPosts"]["total"]);

		//Get Heavy Equipment post
		
		$this->_assign("heavyEquipmentPosts",$data["heavyEquipmentPosts"]["list"]);
		$this->_assign("totalHeavyEquipmentPost",$data["heavyEquipmentPosts"]["total"]);
		/*
		//Get Saloon post
		$saloonPosts = self::_getPostByCategoryID(array(30));
		$this->_assign("saloonPosts",$saloonPosts["list"]);
		$this->_assign("totalSaloonPost",$saloonPosts["total"]);

		//Get Pickup post
		$pickupPosts = self::_getPostByCategoryID(array(31));
		$this->_assign("pickupPosts",$pickupPosts["list"]);
		$this->_assign("totalPickupPost",$pickupPosts["total"]);
		*/

		//Get Bus post
		
		$this->_assign("busPosts",$data["busPosts"]["list"]);
		$this->_assign("totalBusPost",$data["busPosts"]["total"]);
		
		//Get การเกศตร
		
		$this->_assign("farmPosts",$data["farmPosts"]["list"]);
		$this->_assign("totalfarmPost",$data["farmPosts"]["total"]);

		$this->_fetch("index");
	}

	function admin_index(){
		if(!CIUser::checkPermission("admin")){
			show_404();
		}
		$this->_fetch("admin_index");
	}

	private function _getPostByCategoryID($category_id = NULL, $limit = 20) {
		if(empty($category_id)){
			return false;
		}
		$where["where_in"]["cat_id"] = $category_id;
		$where["limit"] = $limit;
		$where["group"] = "pos_id";
		$where["order"] = "pos_update_date DESC";
		$where['where']['status'] = 1;
		$posts = $this->categoryPostModel->get($where);
		unset($where["limit"]);
		$returnPost["total"] = $this->categoryPostModel->count_rows($where);
		$returnPost["list"] = "";
		if(!empty($posts)){
			foreach ($posts as $key => $value) {
				$returnPost["list"][$key] = $value;
				$returnPost["list"][$key]["categories"] = array_reverse(Util::getCategoryInherit($value["cat_id"]));
				$returnPost["list"][$key]["province"] = Util::getProvince($posts[$key]["province"]);
				$returnPost["list"][$key]["type"] = Util::getType($posts[$key]["typ_id"]);
				$returnPost["list"][$key]["images"] = $this->imagesModel->get(array("where"=>array("pos_id"=>$value["id"])));
				$returnPost["list"][$key]["meta"] = Util::getPostMeta($posts[$key]["id"]);
				$returnPost["list"][$key]["comment_count"] = Util::getCommentCount($posts[$key]["typ_id"]);
			}
		}
		return $returnPost;
	}

	function admin_clearcache(){
		if(!CIUser::checkPermission("admin")){
			show_404();
		}
		$this->cache->delete_all();
	}

}